<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="unix,笔记," />










<meta name="description" content="进程终止有8种方式使进程终止，其中5种为征程终止，它们是  从main返回 调用exit 调用_exit或_Exit 最后一个线程从其启动例程返回。 最后一个线程调用pthread_exit  异常终止有3种，它们是  调用abort 接到一个信号并终止。 最后一个线程对取消请求做出响应。  1. exit函数有三个函数用于正常终止一个程序：_exit和_Exit立即进入内核。exit则先执行一些">
<meta property="og:type" content="article">
<meta property="og:title" content="Unix环境高级编程：进程环境">
<meta property="og:url" content="http://liuyun000.gitee.io/2019/11/05/unix/Unix%E7%8E%AF%E5%A2%83%E9%AB%98%E7%BA%A7%E7%BC%96%E7%A8%8B%EF%BC%9A%E8%BF%9B%E7%A8%8B%E7%8E%AF%E5%A2%83/index.html">
<meta property="og:site_name" content="liuyun的博客">
<meta property="og:description" content="进程终止有8种方式使进程终止，其中5种为征程终止，它们是  从main返回 调用exit 调用_exit或_Exit 最后一个线程从其启动例程返回。 最后一个线程调用pthread_exit  异常终止有3种，它们是  调用abort 接到一个信号并终止。 最后一个线程对取消请求做出响应。  1. exit函数有三个函数用于正常终止一个程序：_exit和_Exit立即进入内核。exit则先执行一些">
<meta property="article:published_time" content="2019-11-05T05:17:01.000Z">
<meta property="article:modified_time" content="2019-11-21T05:18:50.923Z">
<meta property="article:author" content="liuyun">
<meta property="article:tag" content="unix">
<meta property="article:tag" content="笔记">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://liuyun000.gitee.io/2019/11/05/unix/Unix环境高级编程：进程环境/"/>





  <title>Unix环境高级编程：进程环境 | liuyun的博客</title>
  








<meta name="generator" content="Hexo 4.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">liuyun的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://liuyun000.gitee.io/2019/11/05/unix/Unix%E7%8E%AF%E5%A2%83%E9%AB%98%E7%BA%A7%E7%BC%96%E7%A8%8B%EF%BC%9A%E8%BF%9B%E7%A8%8B%E7%8E%AF%E5%A2%83/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="liuyun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="liuyun的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Unix环境高级编程：进程环境</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-11-05T13:17:01+08:00">
                2019-11-05
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/unix%E7%8E%AF%E5%A2%83%E9%AB%98%E7%BA%A7%E7%BC%96%E7%A8%8B/" itemprop="url" rel="index">
                    <span itemprop="name">unix环境高级编程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="进程终止"><a href="#进程终止" class="headerlink" title="进程终止"></a>进程终止</h3><p>有8种方式使进程终止，其中5种为征程终止，它们是</p>
<ol>
<li>从main返回</li>
<li>调用exit</li>
<li>调用_exit或_Exit</li>
<li>最后一个线程从其启动例程返回。</li>
<li>最后一个线程调用pthread_exit</li>
</ol>
<p>异常终止有3种，它们是</p>
<ol start="6">
<li>调用abort</li>
<li>接到一个信号并终止。</li>
<li>最后一个线程对取消请求做出响应。</li>
</ol>
<h4 id="1-exit函数"><a href="#1-exit函数" class="headerlink" title="1. exit函数"></a>1. exit函数</h4><p>有三个函数用于正常终止一个程序：_exit和_Exit立即进入内核。exit则先执行一些清理处理（包括调用执行者各终止处理程序，关闭所有标准IO流等），然后进入内核。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">exit</span><span class="params">(<span class="keyword">int</span> status)</span></span>;</span><br><span class="line"><span class="keyword">void</span> _Exit(<span class="keyword">int</span> status);</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> _exit(<span class="keyword">int</span> status);</span><br></pre></td></tr></table></figure>

<blockquote>
<p>exit和_Exit是由ISO C说明的，而_eixt则是由POSIX.1说明的。</p>
</blockquote>
<p>exit函数总是执行一个标准IO库的清理关闭工作：为所有打开流调用fclose函数。这回造成所有缓冲的输出数据被冲洗。  </p>
<p>三个exit函数都带有一个整型参数，称之为终止状态。大多数UNIX shell都提供检查进程终止状态的方法。如果(a)若调动这些函数时不带，或(b)main执行了一个无返回值的return语句，或(c)main没有声明返回类型为整型，则该进程的终止状态是未定义的。但是，若main的返回类型为整型，并且main执行到最后一条语句时返回（隐式返回），那么该进程的终止状态是0.  </p>
<p>main函数赶回一整型值与用该值调用exit是等价的。于是在main函数中exit(0)等价于return(0)。  </p>
<h4 id="2-atexit函数"><a href="#2-atexit函数" class="headerlink" title="2. atexit函数"></a>2. atexit函数</h4><p>按照ISO C的规定，一个进程可以登记多达32个函数，这些函数将由exit自动调用。我们称这些函数为终止处理程序，并调用atexit函数来登记这些函数。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">atexit</span><span class="params">(<span class="keyword">void</span> (*func)(<span class="keyword">void</span>))</span></span>;</span><br><span class="line"><span class="comment">/* 返回值：若成功，返回0；若出错，返回非0 */</span></span><br></pre></td></tr></table></figure>

<p>其中atexit的参数是一个函数地址，当调用此函数时无需向它床底任何参数，也不期望它返回一个值。exit调用这些函数的顺序与它们登记时候的顺序相反。同一函数如若登记多次，则也会被调用多次。  </p>
<p>注意，内核使程序执行的唯一方法是调用一个exec函数。进程自愿终止的唯一方法是显示或隐式地（通过exit）调用_exit或_Exit。进程也可非自愿地由一个信号使其终止。  </p>
<h3 id="环境表"><a href="#环境表" class="headerlink" title="环境表"></a>环境表</h3><p>每个程序都会收到一张环境表。与参数表一样，环境表也是一个字符指针数组，其中每个指针包含一个以null结束的C字符串的地址。全局变量environ为环境指针，指针数组为环境表，其中各指针指向的字符串为环境字符串。   </p>
<p>按照惯例，环境由name=value这样的字符串组成。  </p>
<h3 id="存储器分配"><a href="#存储器分配" class="headerlink" title="存储器分配"></a>存储器分配</h3><p>ISO C说明了三个用于存储空间动态分配的函数。</p>
<ol>
<li>malloc。分配指定字节数的存储区。此存储区中的初始值不确定。</li>
<li>calloc。为指定数量具制定长度的对象分配存储空间。该空间中每一位都初始化为0.</li>
<li>realloc。更改以前分配区的长度（增加或减少）。当增长长度时，可能需将以前分配去的内容移到另一个足够大的区域，以便在尾端提供增加的存储区，而新增区域的初始值则不确定。  </li>
</ol>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> *<span class="title">malloc</span><span class="params">(<span class="keyword">size_t</span> <span class="built_in">size</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span> *<span class="title">calloc</span><span class="params">(<span class="keyword">size_t</span> obj, <span class="keyword">size_t</span> <span class="built_in">size</span>)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">void</span>* <span class="title">realloc</span><span class="params">(<span class="keyword">void</span>* ptr, <span class="keyword">size_t</span> new_size)</span></span>;</span><br><span class="line"><span class="comment">/* 三个函数返回值：若成功则返回非空指针，若出错则返回NULL */</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">free</span><span class="params">(<span class="keyword">void</span>* ptr)</span></span>;</span><br></pre></td></tr></table></figure>

<p>这三个分配函数所返回的指针一定是适当对其的，使其可用于任何数据对象。  </p>
<p>函数free释放ptr指向的存储空间。被释放的空间通常被送入可用存储区池。  </p>
<h3 id="环境变量"><a href="#环境变量" class="headerlink" title="环境变量"></a>环境变量</h3><p>ISO C定义了一个函数getenv，可以用其取环境变量值，但是该标准又称环境的内容是由实现定义的。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">char</span>* <span class="title">getenv</span><span class="params">(<span class="keyword">const</span> <span class="keyword">char</span>* name)</span></span>;</span><br><span class="line"><span class="comment">/* 返回值：指向与name关联的value的指针，若未找到则返回NULL */</span></span><br></pre></td></tr></table></figure>

<p>注意，此函数返回一个指针，它指向name=value字符串中的value。我们应当使用getenv从环境中取一个指定环境变量的值，而不是直接访问environ。  </p>
<p>除了取环境变量，有时也需要设置环境变量。我们可能希望改变现有变量。我们可能虚妄改变现有变量的值，或者增加新的环境变量。不幸的是，并不是所有系统都支持这种能力。  </p>
<table>
<thead>
<tr>
<th>函数</th>
<th>ISO C</th>
<th>POSIX.1</th>
<th>Free BSD 5.2.1</th>
<th>Linux 2.4.22</th>
<th>Mac OS X 10.3</th>
<th>Solaris 9</th>
</tr>
</thead>
<tbody><tr>
<td>getenv</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>putenv</td>
<td></td>
<td>XSI</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>setenv</td>
<td></td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td></td>
</tr>
<tr>
<td>unsetenv</td>
<td></td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td></td>
</tr>
<tr>
<td>clearenv</td>
<td></td>
<td></td>
<td></td>
<td>*</td>
<td></td>
<td></td>
</tr>
</tbody></table>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">putenv</span><span class="params">(<span class="keyword">char</span>* str)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">setenv</span><span class="params">(cosnt <span class="keyword">char</span>* name, <span class="keyword">const</span> <span class="keyword">char</span>* value, <span class="keyword">int</span> rewrite)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">unsetenv</span><span class="params">(<span class="keyword">const</span> <span class="keyword">char</span>* name)</span></span>;</span><br><span class="line"><span class="comment">/* 三个函数的返回值：若成功则返回0，若出错则返回非0值 */</span></span><br></pre></td></tr></table></figure>

<p>这三个函数的操作是：</p>
<ul>
<li><p>putenv取形式为name=value的字符串，将其放到黄竞标中。如果name已经存在，则先删除其原来的定义。</p>
</li>
<li><p>setenv将name设置为value。如果在环境中name已经存在，那么(a)若rewrite非0，则首先删除其现有的定义；(b)若rewrite为0，则不删除其现有定义（name不设置为新的value，而且也不出错）。</p>
</li>
<li><p>unsetenv删除name的定义。即使不存在这种定义也不算出错。  </p>
</li>
</ul>
<h3 id="setjmp和longjmp函数"><a href="#setjmp和longjmp函数" class="headerlink" title="setjmp和longjmp函数"></a>setjmp和longjmp函数</h3><p>在C中，goto语句是不能跨越函数的，而执行这列跳转功能的是函数setjmp和longjmp。这两个函数对于处理发生在深层嵌套函数调用中的出错情况是非常有用的。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;setjmp.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">setjmp</span><span class="params">(jmp_buf env)</span></span>;</span><br><span class="line"><span class="comment">/* 返回值：若直接调用则返回0，若从longjmp调用返回则返回非0值 */</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">longjmp</span><span class="params">(jmp_buf env, <span class="keyword">int</span> val)</span></span>;</span><br></pre></td></tr></table></figure>

<p>setjmp参数env的类型是一个特殊类型jmp_buf。这一数据类型是某种形式的数组，其中存放在调用longjmp时能用来恢复栈状态的所有信息。因为需在另一函数中引用env变量，所以规范的处理方式是将env变量定义为全局变量。  </p>
<p>longjmp函数第一个参数就是在调用setjmp时所用的env，第二个参数是具有非0值的val，它将成为从setjmp处返回的值。使用第二个参数的原因是对于一个setjmp可以有多个longjmp。   </p>
<h4 id="1-自动、寄存器和易失变量"><a href="#1-自动、寄存器和易失变量" class="headerlink" title="1. 自动、寄存器和易失变量"></a>1. 自动、寄存器和易失变量</h4><p>在调用setjmp并使用longjmp返回后，自动变量和寄存器变量状态如何？这些变量的值是否能恢复到以前调用setjmp时的值，或是保持为longjmp调用时的值？不幸的是，对此的回答是“看情况”。大多数实现并不回滚这些自动变量和寄存器变量的值，而所有标准则说它们的值是不确定的。如果你有一个自动变量（就是函数内在栈上分配的变量），而又不想使其回滚，则可定义为具有valatile属性（此关键字可使编译器不对变量进行优化），声明为全局或静态变量的值在执行longjmp时保持不变。  </p>
<h4 id="2-局部变量的潜在问题"><a href="#2-局部变量的潜在问题" class="headerlink" title="2. 局部变量的潜在问题"></a>2. 局部变量的潜在问题</h4><p>为标准IO设置使用setbuf和setvbuf设置缓冲区时，不应使用栈上的数组，因为当分配栈上变量所在函数返回时栈已经重新被新调用的栈帧使用，而标准IO仍使用其缓冲区的存储空间，这就产生了冲突和混乱。为了校正这一问题，应在全局存储空间静态的（如static或exxtern）或者动态地（使用一种alloc函数）为数组databuf分配空间。  </p>
  <br/>

  <br/>

  <br/>

<h3 id="getrlimit和setrlimit函数"><a href="#getrlimit和setrlimit函数" class="headerlink" title="getrlimit和setrlimit函数"></a>getrlimit和setrlimit函数</h3><p>每个进程都有由自愿限制，其中一些可用getrlimit和setrlimit函数查询和更改。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;sys/resource.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">getrlimit</span><span class="params">(<span class="keyword">int</span> resource, struct rlimit* rlptr)</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">setrlimit</span><span class="params">(<span class="keyword">int</span> rseource, <span class="keyword">const</span> struct rlimit* rlptr)</span></span>;</span><br><span class="line"><span class="comment">/* 两个函数返回值：若成功返回0;，若出错返回非0值 */</span></span><br></pre></td></tr></table></figure>

<p>这两个函数在SUS中定义为XSI扩展。进程的资源限制通常是在系统初始化时由进程0建立的，然后由每个后续进程集成。每种实现都可以用自己的方法对各种限制做出调整。  </p>
<p>对这两个函数的每一次调用都会制定一个资源以及一个指向下列结构的指针。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">rlimit</span>&#123;</span></span><br><span class="line">    <span class="keyword">rlim_t</span> rlim_cur;   <span class="comment">/* 软限制：当前的限制值 */</span></span><br><span class="line">    <span class="keyword">rlim_t</span> rlim_max;   <span class="comment">/* 硬限制：rlim_cur的最大值 */</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>



<p>在更改资源限制时，须遵循下列三条规则：</p>
<ol>
<li>任何一个进程都可将一个软限制值更改为小于或等于其硬限制值。</li>
<li>任何一个进程都可降低其硬限制值，但它必须大于或等于其软限制值。这种降低对普通用户而言是不可逆的。</li>
<li>只有超级用户进程可以提高其硬限制值。</li>
</ol>
<p>常量RLIM_INFINTY制定了一个无限量的限制。</p>
<p>这两个函数的resource参数区下列值之一。下表显示那些资源限制是由SUS定义并被讨论的四中系统实现支持的。  </p>
<table>
<thead>
<tr>
<th>限制</th>
<th>XSI</th>
<th>FreeBSD 5.2.1</th>
<th>Linux 2.4.22</th>
<th>Mac OS X 10.3</th>
<th>Solaris 9</th>
</tr>
</thead>
<tbody><tr>
<td>RLIMIT_AS</td>
<td>*</td>
<td></td>
<td>*</td>
<td></td>
<td>*</td>
</tr>
<tr>
<td>RLIMIT_CORE</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>RLIMIT_CPU</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>RLIMIT_DATA</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>RLIMIT_FSIZE</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>RLIMIT_LOCKS</td>
<td></td>
<td></td>
<td>*</td>
<td></td>
<td></td>
</tr>
<tr>
<td>RLIMIT_MEMLOCK</td>
<td></td>
<td>*</td>
<td>*</td>
<td>*</td>
<td></td>
</tr>
<tr>
<td>RLIMIT_NOFILE</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>RLIMIT_NPROC</td>
<td></td>
<td>*</td>
<td>*</td>
<td>*</td>
<td></td>
</tr>
<tr>
<td>RLIMIT_RSS</td>
<td></td>
<td>*</td>
<td>*</td>
<td>*</td>
<td></td>
</tr>
<tr>
<td>RLIMIT_SBSIZE</td>
<td></td>
<td>*</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>RLIMIT_STACK</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
<td>*</td>
</tr>
<tr>
<td>RLIMIT_VMEM</td>
<td></td>
<td>*</td>
<td></td>
<td></td>
<td>*</td>
</tr>
</tbody></table>
<ul>
<li><p>RLIMIT_AS：进程可用存储区的最大总长度（字节）。这会影响sbrk函数和mmap函数。</p>
</li>
<li><p>RLIMIT_CORE：core文件的最大字节数，若其值为0则组织创建core文件。</p>
</li>
<li><p>RLIMIT_CPU：CPU时间的最大值（秒），当超过此软限制时，该向该进程发送SIGXCPU信号。</p>
</li>
<li><p>RLIMIT_DATA：数据段的最大字节长度。这是初始化数据、非初始化以及堆的总和</p>
</li>
<li><p>RLIMIT_FSIZE：可以创建的文件的最大字节长度。当唱过此软限制时，该向该进程发送SIGXFSZ信号。</p>
</li>
<li><p>RLIMIT_LOCKS：一个进程可持有的文件锁的最大数（此数也包括Linux特有的文件租借数）。</p>
</li>
<li><p>RLIMIT_MEMLOCK：一个进程使用mlock能够锁定在存储器中的最大字节长度。</p>
</li>
<li><p>RLIMIT_NOFILE：每个进程能打开的最大文件数。更改此限制将影响到sysconfig函数在参数_SC_OPEN_MAX中返回的值。</p>
</li>
<li><p>RLIMIT_NPROC：每个实际用户ID可拥有的最大子进程数。更改此限制将影响到sysconf函数在参数_SC_CHILD_MAX中返回的值.</p>
</li>
<li><p>RLIMIT_RSS：最大驻内存集的自己长度。如果物理存储器供不应求，则内核从进程处收回超过RSS的部分。</p>
</li>
<li><p>RLIMIT_SBSIZE：用户在任一给定时刻都可以占用的套接字缓冲区的最大长度（字节）。</p>
</li>
<li><p>RLIMIT_STACK：栈的最大字节长度。</p>
</li>
<li><p>RLIMIT_VMEM：这是RLIMIT_AS的同义词。</p>
</li>
</ul>
<p>资源限制影响到调用进程并由其子进程继承。这就意味着为了影响一个用户的所有后续进程，需将资源限制的设置构造在shell之中。确实，Bourne shell、GUN Bourne-again shell和Korn shell具有内置的ulimit命令，C shell具有内置的limit命令。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/unix/" rel="tag"># unix</a>
          
            <a href="/tags/%E7%AC%94%E8%AE%B0/" rel="tag"># 笔记</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/11/03/unix/unix%E7%8E%AF%E5%A2%83%E9%AB%98%E7%BA%A7%E7%BC%96%E7%A8%8B%EF%BC%9A%E6%A0%87%E5%87%86IO%E5%BA%93/" rel="next" title="unix环境高级编程：标准IO库">
                <i class="fa fa-chevron-left"></i> unix环境高级编程：标准IO库
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/11/22/%E5%AF%86%E7%A0%81%E5%AD%A6/miracl%E5%BA%93%E5%AE%9E%E7%8E%B0ElGamal%E5%8A%A0%E8%A7%A3%E5%AF%86/" rel="prev" title="miracl库实现ElGamal加解密">
                miracl库实现ElGamal加解密 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">liuyun</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%20%7C%7C%20archive">
              
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">9</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">21</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#进程终止"><span class="nav-number">1.</span> <span class="nav-text">进程终止</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-exit函数"><span class="nav-number">1.1.</span> <span class="nav-text">1. exit函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-atexit函数"><span class="nav-number">1.2.</span> <span class="nav-text">2. atexit函数</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#环境表"><span class="nav-number">2.</span> <span class="nav-text">环境表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#存储器分配"><span class="nav-number">3.</span> <span class="nav-text">存储器分配</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#环境变量"><span class="nav-number">4.</span> <span class="nav-text">环境变量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#setjmp和longjmp函数"><span class="nav-number">5.</span> <span class="nav-text">setjmp和longjmp函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-自动、寄存器和易失变量"><span class="nav-number">5.1.</span> <span class="nav-text">1. 自动、寄存器和易失变量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-局部变量的潜在问题"><span class="nav-number">5.2.</span> <span class="nav-text">2. 局部变量的潜在问题</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#getrlimit和setrlimit函数"><span class="nav-number">6.</span> <span class="nav-text">getrlimit和setrlimit函数</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">liuyun</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/miku.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
